using System;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for SQLServerProviders
/// </summary>
public partial class SQLServerProviders
{    
    
	public string[] LoginAuthentication(
	string email, string password)
	{
	    errorMessage="";
        string[] valueToGet = new string[3]{"","",""};
	    try
	    {
	        OpenConnection();
	        sqlCommand = new SqlCommand();
	        sqlCommand.CommandText="SPLogin";
	        sqlCommand.Connection=sqlConnection;
	        sqlCommand.CommandType= 
	        CommandType.StoredProcedure;
            sqlCommand.Parameters.AddWithValue(
            "@Type", "0");
	        sqlCommand.Parameters.AddWithValue(
	        "@Email",email);
	        sqlDataReader = 
	        sqlCommand.ExecuteReader();
	        if(sqlDataReader.Read())
	        {
	            Common cls = new Common();
	            cls.Key= "rcv";
	            if(Convert.ToString(
                sqlDataReader["Password"]) == 
                cls.RCVEncrypting(password))
	            {
	                valueToGet[0] = "OK";
	                valueToGet[1]=Convert.ToString(
	                sqlDataReader["UserID"]);
                    valueToGet[2] = Convert.ToString(
                    sqlDataReader["FullName"]); 
                }
                else
                    valueToGet[0] = "P";
	        }
	        else
	        {
                valueToGet[0] = "U";
	        }
	        sqlDataReader.Close();
	        sqlDataReader.Dispose();
             
            CloseConnection();          
	    }
	    catch(Exception ex)
	    {
	        errorMessage=ex.Message;
	    }
	    return valueToGet;
	}
	
    public bool ChangePassword(
    string email, string OldPassword, string newPassword)
    {
        errorMessage = "";
        bool changePasssord=true;
        try
        {
            OpenConnection();
            sqlCommand = new SqlCommand();
            sqlCommand.CommandText = "SPChangePassword";
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandType =
            CommandType.StoredProcedure;
            Common cls = new Common();
            cls.Key="rcv";
            sqlCommand.Parameters.AddWithValue(
            "@Type", "0");
            sqlCommand.Parameters.AddWithValue(
            "@Email", email);
            sqlCommand.Parameters.AddWithValue(
            "@OldPassword", cls.RCVEncrypting(OldPassword  ));
            sqlCommand.Parameters.AddWithValue(
            "@NewPassword", cls.RCVEncrypting(newPassword ));
            SqlParameter sqlParameter = new SqlParameter("ChangePassword","0");
            sqlParameter.Direction = ParameterDirection.ReturnValue;
            sqlCommand.Parameters.Add(sqlParameter);
            sqlCommand.ExecuteScalar();
            changePasssord = (Convert.ToString(
            sqlParameter.Value) == "1");
             

            CloseConnection();
        }
        catch (Exception ex)
        {
            errorMessage = ex.Message;
        }
        return changePasssord;
    }
    public string ForgotPassword(
    string email, string fullName, string dateOfBirth)
    {
        errorMessage = "";
        string forgotPasssord = "";
        try
        {
            OpenConnection();
            sqlCommand = new SqlCommand();
            sqlCommand.CommandText = "SPForgotPassword";
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandType =
            CommandType.StoredProcedure;
            sqlCommand.Parameters.AddWithValue(
            "@Type", "0");
            sqlCommand.Parameters.AddWithValue(
            "@Email", email);
            sqlCommand.Parameters.AddWithValue(
            "@FullName", fullName);
            sqlCommand.Parameters.AddWithValue(
            "@DateOfBirth", dateOfBirth);
            forgotPasssord = Convert.ToString(
            sqlCommand.ExecuteScalar())  ;
            Common cls = new Common();
            cls.Key="rcv";
            forgotPasssord=cls.RCVEncrypting(forgotPasssord );
            CloseConnection();
        }
        catch (Exception ex)
        {
            errorMessage = ex.Message;
        }
        return forgotPasssord;
    }
	
}
